﻿using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using churchtool.Operations.Commands;
using churchtool.View;
using System;
using System.IO;
using System.Reflection;
using churchtool.Plugin.Interfaces.ViewModel;
using churchtool.Plugin.Interfaces;
using System.ComponentModel.Composition;
using churchtool.Operations;
using System.ComponentModel.Composition.Hosting;
using System.Collections;


namespace churchtool.ViewModel
{
    class MainWindowViewModel : ViewModelBase
    {
            #region Fields

            private ICommand _changePageCommand;
            private ICommand _showHelp;
            private ICommand _showAboutSermonUpload;
            private ICommand _reportProblem;

            private AboutWindowView aboutWindow;

            private List<IPluginInterface> _pluginList;

            private IPageViewModel _currentPageViewModel;

            private List<IPageViewModel> _models;

            #endregion

            public MainWindowViewModel()
            {
                string pluginsPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), LoadPluginCommand.PLUGIN_PATH_CONSTANT);
                InitPlugins(pluginsPath);
                if (_models.Capacity > 0)
                    _currentPageViewModel = _models[0];
            }

            #region Properties / Commands

            /// <summary>
            /// The file browse message to be displayed in the View.
            /// </summary>
            public string SermonUploadTitle
            {
                get
                {
                    return "SermonUpload --- " + Assembly.GetExecutingAssembly().GetName().Version;
                }

                set { }
            }

            public ICommand ChangePageCommand
            {
                get
                {
                    if (_changePageCommand == null)
                    {
                        _changePageCommand = new RelayCommand(
                            p => ChangeViewModel((IPageViewModel)p),
                            p => p is IPageViewModel);
                    }

                    return _changePageCommand;
                }
            }

            public List<IPageViewModel> PageViewModels
            {
                get
                {
                    return _models;
                }
            }

            public IPageViewModel CurrentPageViewModel
            {
                get
                {
                    return _currentPageViewModel;
                }
                set
                {
                    if (_currentPageViewModel != value)
                    {
                        _currentPageViewModel = value;
                        RaisePropertyChangedEvent("CurrentPageViewModel");
                    }
                }
            }

            public ICommand ShowHelp
            {
                get
                {
                    if (this._showHelp == null)
                    {
                        this._showHelp = new RelayCommand(parm => m_ShowHelp());
                    }
                    return this._showHelp;
                }
            }

            private void m_ShowHelp()
            {
                System.Diagnostics.Process.Start(Directory.GetCurrentDirectory() + "\\Doc\\manual.pdf");
            }

            public ICommand ReportProblem
            {
                get
                {
                    if (this._reportProblem == null)
                    {
                        this._reportProblem = new RelayCommand(parm => m_ReportProblem());
                    }
                    return this._reportProblem;
                }
            }

            private void m_ReportProblem()
            {
                try
                {
                    System.Diagnostics.Process.Start("mailto:chris@fcg-rv.de");
                }
                catch
                {
                    MessageBox.Show("Please report Bugs to chris@fcg-rv.de", "Mail Error", MessageBoxButton.OK, MessageBoxImage.Warning);
                }
            }

            public ICommand ShowAboutSermonUpload
            {
                get
                {
                    if (this._showAboutSermonUpload == null)
                    {
                        this._showAboutSermonUpload = new RelayCommand(parm => m_ShowAboutSermonUpload());
                    }
                    return this._showAboutSermonUpload;
                }
            }

            private void m_ShowAboutSermonUpload()
            {
                if (aboutWindow == null)
                    aboutWindow = new AboutWindowView();
                aboutWindow.Show();
            }


            #endregion

            #region Methods

            private void ChangeViewModel(IPageViewModel viewModel)
            {
                if (!PageViewModels.Contains(viewModel))
                    PageViewModels.Add(viewModel);

                CurrentPageViewModel = PageViewModels
                    .FirstOrDefault(vm => vm == viewModel);
            }

            private void InitPlugins(string folder)
            {
                string[] files = Directory.GetFiles(folder, "*.dll");
                _pluginList = new List<IPluginInterface>();

                foreach (string file in files)
                {
                    Assembly assembly = Assembly.LoadFile(file);
                    foreach (Type type in assembly.GetTypes())
                    {
                        if (!type.IsClass || type.IsNotPublic) continue;
                        Type[] interfaces = type.GetInterfaces();
                        if (((IList)interfaces).Contains(typeof(IPluginInterface)))
                        {
                            object obj = Activator.CreateInstance(type);
                            IPluginInterface t = (IPluginInterface)obj;
                            _pluginList.Add(t);
                        }
                    }
                }

                _models = new List<IPageViewModel>();
                foreach (IPluginInterface _ipluginInterface in _pluginList)
                {
                    _models.Add(_ipluginInterface.PluginMainWindowViewModel());
                }
            }

            #endregion
    }
}
